In this article I am going to explain How to create DropDownList using MVC .net. Here, I am binding the MVC dropdownlist from model object (myDatabaseEntity) using entity framework. You can assign the model object to the viewBag property (Category) and bind on the Razor view using the MVC HTML dropdownlist control.
Create an MVC application in Visual studio.
Create controller:
Right click on the controller folder and click on add and name it as Home.
private myDatabaseEntities db = new myDatabaseEntities();
public ActionResult Index()
{
ViewBag.Category = new SelectList(db.Categories, "CategoryId", "Name");
return View();
}
Create a view Index.cshtml:
Right click on the index view and Create a view. Copy and paste the following code.
Select Department: @Html.DropDownList("Category", "Select Category")
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article